import { defineStore } from 'pinia'
import { ref, reactive } from 'vue'
import { requirementAPI } from '@/utils/api'

export const useRequirementStore = defineStore('requirement', () => {
  // 当前需求数据
  const currentRequirement = reactive({
    core_goal: '',
    target_audience: '',
    budget_per_person: '',
    focus_point: '',
    gift_type_preference: '',
    specific_product: '',
    customer_preference: '',
    customer_pain_point: ''
  })
  
  // 交互状态
  const interactionState = reactive({
    currentContent: '',
    contentHistory: [],
    isGenerating: false,
    userFeedback: '',
    iterationCount: 0,
    isConfirmed: false
  })
  
  // 提交需求
  const submitRequirement = async (data) => {
    try {
      const response = await requirementAPI.submitRequirement(data)
      Object.assign(currentRequirement, data)
      return response
    } catch (error) {
      console.error('提交需求失败:', error)
      throw error
    }
  }
  
  // 保存内容版本
  const saveVersion = (content, feedback) => {
    interactionState.contentHistory.push({
      id: Date.now(),
      content: content,
      feedback: feedback,
      timestamp: new Date(),
      version: interactionState.iterationCount + 1
    })
    interactionState.iterationCount++
    interactionState.currentContent = content
  }
  
  // 回退到指定版本
  const rollbackToVersion = (versionId) => {
    const targetVersion = interactionState.contentHistory.find(v => v.id === versionId)
    if (targetVersion) {
      interactionState.currentContent = targetVersion.content
    }
  }
  
  // 确认方案
  const confirmSolution = () => {
    if (interactionState.currentContent.length > 0) {
      interactionState.isConfirmed = true
      return true
    }
    return false
  }
  
  // 检查是否可以进入Kimi页面
  const canProceedToKimi = () => {
    return interactionState.isConfirmed && interactionState.currentContent.length > 0
  }
  
  return {
    currentRequirement,
    interactionState,
    submitRequirement,
    saveVersion,
    rollbackToVersion,
    confirmSolution,
    canProceedToKimi
  }
})